package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import java.util.Calendar;
import java.util.Date;

public class CDateVersion
{
  private final int I_FIRST_YEAR = 1900;
  private final int I_FIRST_DOUBLE_NUMBER = 10;

  private String FillLeftZeros(String sIn, int iLength)
  {
    for (; sIn.length() < iLength;)
      sIn = "0" + sIn;
    return sIn;
  }

  public String DateToXMLDateTimeString(Date oDate)
  {
    String sResult = "0001-01-01T00:00:00.0000000+02:00";
    String sYear = "";
    String sMonth = "";
    String sDay = "";
    String sHour = "";
    String sMinute = "";
    String sSecond = "";
    String sMillisecond = "";
    String sGMT = "+00:00";
    Calendar oCalendar = Calendar.getInstance();
    oCalendar.setTime(oDate);
    int i = 0;
    if (oDate != null)
    {
      sYear = FillLeftZeros("" + (oCalendar.get(Calendar.YEAR)), 4);
      sMonth = FillLeftZeros("" + (oCalendar.get(Calendar.MONTH) + 1), 2);
      sDay = FillLeftZeros("" + oCalendar.get(Calendar.DAY_OF_MONTH), 2);
      sHour = FillLeftZeros("" + oCalendar.get(Calendar.HOUR_OF_DAY), 2);
      sMinute = FillLeftZeros("" + oCalendar.get(Calendar.MINUTE), 2);
      sSecond = FillLeftZeros("" + oCalendar.get(Calendar.SECOND), 2);
      sGMT = oCalendar.getTimeZone().getID().substring(3);
      if (!CUtilityMethods.IsValuable(sGMT))
      {
        sGMT = "+00:00";
      }
      sMillisecond = FillLeftZeros("", 7);
      sResult =
        sYear
          + "-"
          + sMonth
          + "-"
          + sDay
          + "T"
          + sHour
          + ":"
          + sMinute
          + ":"
          + sSecond
          + "."
          + sMillisecond
          + sGMT;
    }
    return sResult;
  }

  public String GetTodayDateVersion()
  {
    Calendar oCalendar = Calendar.getInstance();
    String sResult = "";
    int i = 0;
    sResult = sResult + (oCalendar.get(Calendar.YEAR));
    i = (oCalendar.get(Calendar.MONTH) + 1);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    i = oCalendar.get(Calendar.DAY_OF_MONTH);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    return sResult;
  }

  public String GetWeekDayVersion()
  {
    Calendar oCalendar = Calendar.getInstance();
    String sResult = "";
    int i = 0;
    i = oCalendar.get(Calendar.DAY_OF_WEEK);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    return sResult;
  }

  public String GetTimeVersion()
  {
    Calendar oCalendar = Calendar.getInstance();
    String sResult = "";
    int i = 0;
    i = oCalendar.get(Calendar.HOUR_OF_DAY);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    i = oCalendar.get(Calendar.MINUTE);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    i = oCalendar.get(Calendar.SECOND);
    if (i < I_FIRST_DOUBLE_NUMBER)
    {
      sResult = sResult + "0";
    }
    sResult = sResult + i;
    return sResult;
  }

}
